package com.fare.fdc.action;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentInformation;
import org.apache.pdfbox.util.PDFTextStripper;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.stereotype.Controller;

import com.fare.taluo.BaseFdcAction;
import com.fare.taluo.application.plane.city.PortApplication;
import com.fare.taluo.application.plane.flight.FlightInfoApplication;
import com.fare.taluo.util.DateUtils;
import com.fare.taluo.vo.core.FlightInfoVO;
import com.fare.taluo.vo.core.PortEntityVO;

@Controller("shenHangParseAction")
@Namespace("/")
public class ShenHangParseAction extends BaseFdcAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1272743003770950319L;
	@Resource(name="flightInfoApplication")
	private FlightInfoApplication application;
	@Resource(name="portApplication")
	private PortApplication portApplication;
	private Map<String,PortEntityVO> voMap;
	private  String uploadFileZH;
	
	public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

	/**
	 * 
	 * 解析pdf文档信息
	 * 
	 * @param pdfPath
	 *            pdf文档路径
	 * 
	 * @throws Exception
	 */
	public void pdfParse() throws Exception

	{   
		/*String pdfPath = "c://深圳航空.pdf";*/
		InputStream input = null;	
		PDDocument document = null;

		try {
            if(null!=uploadFileZH){
            	input = new FileInputStream(uploadFileZH);
            	
            }else{
            	super.toJSONOut("{\"result\":\"解析失败!\"}");
            }
			
            voMap = portApplication.findPortEntityToMapByName();
			// 加载 pdf 文档

			document = PDDocument.load(input);

			/** 文档属性信息 **/

			PDDocumentInformation info = document.getDocumentInformation();

			// 获取内容信息
            String companyCode = "ZH";
			PDFTextStripper pts = new PDFTextStripper();
			String content = pts.getText(document);
			List<List> listlist = new ArrayList<List>();
			// 根据"ZH"将字符串分隔
			String[] records = content.split(companyCode);
			// 从第二个字符串开始计算
			for (int i = 1; i < records.length; i++) {
				// 获得单条记录
				String str = records[i].replaceAll("\r\n", "");
				String newString = companyCode + str;
				String[] arrayString = newString.split(" ");
				List<String> listOne = new ArrayList<String>();
				List<String> listTwo = new ArrayList<String>();
				String companyCodeOne = null;
				String companyCodeTwo = null;
				if (arrayString[0].contains("/")) {
					String[] companyCodes = arrayString[0].split("/");
					companyCodeOne = companyCodes[0];
					if (companyCodes[1].length() > 1) {
						companyCodeTwo = companyCodes[0].substring(0,
								companyCodes[0].length() - 2) + companyCodes[1];
					} else {
						companyCodeTwo = companyCodes[0].substring(0,
								companyCodes[0].length() - 1) + companyCodes[1];
					}

				} else {
					companyCodeOne = arrayString[0];
					companyCodeTwo = arrayString[0];
				}

				listOne.add(companyCodeOne);
				listOne.add(arrayString[1]);
				listOne.add(arrayString[2]);
				listOne.add(arrayString[3]);
				listOne.add(arrayString[4]);
				listOne.add(arrayString[5]);

				listTwo.add(companyCodeTwo);
				listTwo.add(arrayString[1]);
				listTwo.add(arrayString[5]);
				listTwo.add(arrayString[6]);
				listTwo.add(arrayString[7]);
				listTwo.add(arrayString[8]);

				listlist.add(listOne);
				listlist.add(listTwo);

			}
			
		   //数据验证判断并入库
			for (Iterator iterator = listlist.iterator(); iterator.hasNext();) {
				List<String> list = (List) iterator.next();
				FlightInfoVO vo = new FlightInfoVO();
				//PortEntityVO portVO =  portApplication.findPortEntityByCityName(list.get(2));
				PortEntityVO portVO = voMap.get(list.get(2));
				//PortEntityVO portVOToCity =  portApplication.findPortEntityByCityName(list.get(5));
				PortEntityVO portVOToCity = voMap.get(list.get(5));
				if(portVO != null && portVOToCity != null){
					if(portVO.getPortCode().equals("NAY")){
						portVO.setPortCode("PEK");
					}
					if(portVOToCity.getPortCode().equals("NAY")){
						portVOToCity.setPortCode("PEK");
					}
					vo.setFlightNo(list.get(0)); 
					 vo.setFrequence(list.get(1).replaceAll("[.]", ""));
					 
					 if(list.get(2).equals("北京")){
						 vo.setFromCityName(list.get(2)+"(PEK)");
					 }else{
						 vo.setFromCityName(list.get(2));
					 }
					 
					
					 if(list.get(3).toCharArray().length>4){
						 vo.setFlightStartTime(list.get(3));
					 }else{
						 vo.setFlightStartTime("0"+list.get(3));
					 }
					 if(list.get(4).toCharArray().length>4){
						 vo.setFlightEndTime(list.get(4));
					 }else{
						 vo.setFlightEndTime("0"+list.get(4));
					 }
					 
					 if(list.get(5).equals("北京")){
						 vo.setToCityName(list.get(5)+"(PEK)");
					 }else{
						 vo.setToCityName(list.get(5));
					 }
					 
					 vo.setFromCity(portVO.getPortCode());
					 try {
						 vo.setToCity(portVOToCity.getPortCode());
					} catch (Exception e) {
						e.printStackTrace();
					}
					
					 vo.setAreaCode(portVO.getAreaCode());
					 vo.setAreaName(portVO.getAreaName());
					 vo.setCompanyCode(companyCode);
					 vo.setCompanyName("深圳航空");
					 vo.setEntryTime(new Date());
					 vo.setFlightDate(DateUtils.dateToStr(new Date(), "MM-dd"));
					 try {
						 application.save(vo);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			
			}
			super.toJSONOut("{\"result\":\"解析成功!\"}");
		} catch (Exception e){

			e.printStackTrace();
			super.toJSONOut("{\"result\":\"解析失败!请通知管理员\"}");
		} finally {

			if (null != input)

				input.close();

			if (null != document)

				document.close();

		}

	}

	

	public static void main(String[] args) throws Exception {
        System.out.println("dddd");
	}

	public String getUploadFileZH() {
		return uploadFileZH;
	}



	public void setUploadFileZH(String uploadFileZH) {
		this.uploadFileZH = uploadFileZH;
	}

	


	
}
